package org.vehicle.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.vehicle.entity.Blacklist;
import org.vehicle.entity.security.MyUserDetail;
import org.vehicle.service.BlacklistService;


@Controller
public class BlackListController {
	private static Logger logger = LogManager.getLogger(BlackListController.class.getName());
	@Autowired
	private BlacklistService blacklistService;
	
	@RequestMapping("blacklistInfo")
	public String blacklistInfo(HttpServletRequest req, Model model){
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		model.addAttribute("manager", ud);
		return "blackList";
	}
	/**
	 * 获取黑名单分页
	 * @param request
	 * @return
	 */
	@RequestMapping("/getBlacklistInfoPage")
    @ResponseBody
    public Map<String, Object> getBlacklistInfoPage(HttpServletRequest request) {
    	Map<String, Object> map = new HashMap<String, Object>();
        String currentPage = request.getParameter("curr");
        String pageSize = request.getParameter("pageSize");
        String personName = request.getParameter("personName");
        String certificateCode = request.getParameter("certificateCode");
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("startPage", (Integer.parseInt(currentPage) - 1) * Integer.parseInt(pageSize));
            paramMap.put("pageSize", Integer.parseInt(pageSize));
            paramMap.put("personName", personName);
            paramMap.put("certificateCode", certificateCode);
            map = blacklistService.getBlacklistPage(paramMap);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        return map;
    }
	
	/**
	 * 获取黑名单信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/getBlacklistInfo")
    @ResponseBody
    public Map<String, Object> getBlacklistInfo(HttpServletRequest request) {
    	Map<String, Object> map = new HashMap<String, Object>();
    	String blackListId=request.getParameter("blackListId");
    	Blacklist blacklist=blacklistService.selectByPrimaryKey(Long.parseLong(blackListId));
    	map.put("blackListInfo", blacklist);
        return map;
    }
	/**
	 * 添加黑名单
	 * @param req
	 * @return
	 */
	@RequestMapping("/addBlackList")
	@ResponseBody
	public Map<String, Object> addBlackList(Blacklist blackList){
		Map<String, Object> paramMap=new HashMap<String, Object>();
		try{
			Map<String, Object> paramMap2=new HashMap<String, Object>();
			paramMap2.put("certificateCode", blackList.getCertificateCode());
			Blacklist testBlacklist=blacklistService.findBlacklistByCertificateCode(paramMap2);
			if(testBlacklist!=null){
				paramMap.put("isSuccess", 0);
				paramMap.put("errMsg", "修改失败，身份证已经存在");
			}else{
				blackList.setCreateDate(new Date());
				blackList.setBlackListstate(1);
				blacklistService.insertSelective(blackList);
				paramMap.put("isSuccess", 1);
				paramMap.put("errMsg", null);
			}
		}catch(Exception e){
			logger.error("添加黑名单异常", e);
			paramMap.put("isSuccess", 0);
			paramMap.put("errMsg", "添加失败");
		}
		return paramMap;
	}
	
	/**
	 * 修改黑名单
	 * @param req
	 * @return
	 */
	@RequestMapping("/updateBlackList")
	@ResponseBody
	public Map<String, Object> updateBlackList(Blacklist blackList){
		Map<String, Object> paramMap=new HashMap<String, Object>();
		try{
			Map<String, Object> paramMap2=new HashMap<String, Object>();
			paramMap2.put("certificateCode", blackList.getCertificateCode());
			paramMap2.put("blackListId", blackList.getBlackListId());
			Blacklist testBlacklist=blacklistService.findBlacklistByCertificateCode(paramMap2);
			if(testBlacklist!=null){
				paramMap.put("isSuccess", 0);
				paramMap.put("errMsg", "修改失败，身份证已经存在");
			}else{
				blacklistService.updateByPrimaryKeySelective(blackList);
				paramMap.put("isSuccess", 1);
				paramMap.put("errMsg", null);
			}
		}catch(Exception e){
			logger.error("修改黑名单异常", e);
			paramMap.put("isSuccess", 0);
			paramMap.put("errMsg", "修改失败");
		}
		return paramMap;
	}
	
	/**
	 * 删除黑名单
	 * @param req
	 * @return
	 */
	@RequestMapping("/delBlackList")
	@ResponseBody
	public Map<String, Object> delBlackList(Blacklist blackList){
		Map<String, Object> paramMap=new HashMap<String, Object>();
		try{
			blacklistService.deleteByPrimaryKey(blackList.getBlackListId());
			paramMap.put("isSuccess", 1);
			paramMap.put("errMsg", null);
		}catch(Exception e){
			logger.error("修改黑名单异常", e);
			paramMap.put("isSuccess", 0);
			paramMap.put("errMsg", "修改失败");
		}
		return paramMap;
	}
}
